package com.zy;

import java.sql.*;
import java.util.Scanner;

public class petstore {
    public static boolean login(String username,String password){
        try(Connection connection=DataSourceUtils.getConnection()){
            Statement statement=connection.createStatement();
            ResultSet resultSet=statement.executeQuery("select * from User where username = '"+username +"' and password = '"+password+"' and is_admin =TRUE");
            if(resultSet.next()){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  false;
    }
    public static boolean login1(String username, String password) {
        try(Connection connection =DataSourceUtils.getConnection()) {
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from User where username = '"+username +"' and password = '"+password+"' and is_admin =FALSE");
            if(resultSet.next()){
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    public static void insert(){
        try(Connection conn=DataSourceUtils.getConnection()){
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入宠物主人id");
            int owner_id=scanner.nextInt();
            scanner.nextLine();
            System.out.println("请输入宠物昵称：");
            String pet_name = scanner.nextLine();
            System.out.println("请输入宠物类型：");
            String type = scanner.nextLine();
            System.out.println("请输入宠物年龄：");
            int age = scanner.nextInt();
            scanner.nextLine();
            System.out.println("请输入宠物性别：");
            String gender = scanner.nextLine();
            System.out.println("请输入宠物价格：");
            int price = scanner.nextInt();
            scanner.nextLine();
            PreparedStatement statement = conn.prepareStatement(" insert into  Pet(owner_id,pet_name,type,age,gender,price)"+"values(?,?,?,?,?,?)");
            statement.setInt(1,owner_id);
            statement.setString(2,pet_name);
            statement.setString(3,type);
            statement.setInt(4,age);
            statement.setString(5,gender);
            statement.setInt(6,price);
            int rows =statement.executeUpdate();
            if(rows>0){
                System.out.println("添加成功");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void delete(){
        try(Connection connection =DataSourceUtils.getConnection()) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入要删除的宠物的id:");
            int id = scanner.nextInt();
            PreparedStatement statement = connection.prepareStatement("delete from Pet where id = ?");
            statement.setInt(1,id);
            int rows = statement.executeUpdate();
            if (rows > 0) {
                System.out.println("删除成功");
            }
            else {
                System.out.println("删除失败");
            }
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

    public static void update(){
        try(Connection connection=DataSourceUtils.getConnection()){
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入宠物id");
            int id = scanner.nextInt();
            scanner.nextLine();
            System.out.println("请输入主人id：");
            int owner_id = scanner.nextInt();
            scanner.nextLine();
            System.out.println("请输入宠物昵称：");
            String pet_name = scanner.nextLine();
            System.out.println("请输入宠物类型：");
            String type = scanner.nextLine();
            System.out.println("请输入宠物年龄：");
            int age = scanner.nextInt();
            scanner.nextLine();
            System.out.println("请输入宠物性别：");
            String gender = scanner.nextLine();
            System.out.println("请输入宠物价格：");
            int price = scanner.nextInt();
            scanner.nextLine();
            PreparedStatement statement = connection.prepareStatement("update pets set owner_id = ?,pet_name = ?,type = ?,age = ?,gender = ?,price = ? where id = ?");
            statement.setInt(1,owner_id);
            statement.setString(2,pet_name);
            statement.setString(3,type);
            statement.setInt(4,age);
            statement.setString(5,gender);
            statement.setInt(6,price);
            statement.setInt(7,id);
            int rows =statement.executeUpdate();
            if(rows>0){
                System.out.println("修改成功");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }
    public static void select(){
       try(Connection connection=DataSourceUtils.getConnection()) {
           System.out.println(connection);
           Statement statement = connection.createStatement();
           System.out.println(statement);
           ResultSet resultSet = statement.executeQuery("select * from Pet order by price desc ");
           while (resultSet.next()){
               System.out.println("=============================");
               System.out.println("id:"+resultSet.getInt("id"));
               System.out.println("主人id:"+resultSet.getInt("owner_id"));
               System.out.println("昵称:"+resultSet.getString("pet_name"));
               System.out.println("类型:"+resultSet.getString("type"));
               System.out.println("年龄:"+resultSet.getInt("age"));
               System.out.println("性别:"+resultSet.getString("gender"));
               System.out.println("价格:"+resultSet.getString("price"));
           }

       } catch (SQLException e) {
           throw new RuntimeException(e);
       }
    }
    public static void select2(){
        try(Connection connection= DataSourceUtils.getConnection()) {
            System.out.println(connection);
            Statement statement = connection.createStatement();
            System.out.println(statement);
            ResultSet resultSet = statement.executeQuery("select * from Pet order by age desc ");
            while (resultSet.next()){
                System.out.println("=============================");
                System.out.println("id:"+resultSet.getInt("id"));
                System.out.println("主人id:"+resultSet.getInt("owner_id"));
                System.out.println("昵称:"+resultSet.getString("pet_name"));
                System.out.println("类型:"+resultSet.getString("type"));
                System.out.println("年龄:"+resultSet.getInt("age"));
                System.out.println("性别:"+resultSet.getString("gender"));
                System.out.println("价格:"+resultSet.getString("price"));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void buy(){
        try (Connection connection = DataSourceUtils.getConnection()) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入宠物id：");
            int id = scanner.nextInt();
            scanner.nextLine();
            PreparedStatement statement1 = connection.prepareStatement("select * from Pet where id = ?");
            statement1.setInt(1,id);
            ResultSet resultSet1 = statement1.executeQuery();
            while(resultSet1.next()){
                System.out.println("id:"+resultSet1.getInt("id"));
                System.out.println("主人id:"+resultSet1.getInt("owner_id"));
                System.out.println("昵称:"+resultSet1.getString("pey_name"));
                System.out.println("类型:"+resultSet1.getString("type"));
                System.out.println("年龄:"+resultSet1.getInt("age"));
                System.out.println("性别:"+resultSet1.getString("gender"));
                System.out.println("价格:"+resultSet1.getString("price"));
            }
            System.out.println("请输入主人id：");
            int owner_id = scanner.nextInt();
            scanner.nextLine();
            PreparedStatement statement2 = connection.prepareStatement("update pets set owner_id = ? where id = ?");
            statement2.setInt(1,owner_id);
            statement2.setInt(2,id);
            int rows =statement2.executeUpdate();
            if (rows > 0) {
                System.out.println("修改成功");
            }
            PreparedStatement statement3 = connection.prepareStatement("update user set integral = integral - 5 where id = ?");
            statement2.setInt(1,owner_id);
            int rows1 =statement2.executeUpdate();
            if (rows1 > 0) {
                System.out.println("扣分成功");
            }
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入密码：");
        String password = scanner.nextLine();
        if(login(username,password)){
            System.out.println("您是管理员!");
            while (true){
                System.out.println("请选择要执行的操作：1）添加宠物 2）删除宠物 3）修改宠物 4)退出");
                int num = scanner.nextInt();
                if (num == 1){
                    insert();
                }else if (num == 2){
                    delete();
                }else if (num == 3){
                    update();
                }else if (num == 4){
                    break;
                }else {
                    System.out.println("请输入1~4的数字！！");
                }
            }
        }
        else if(login1(username,password)){
            System.out.println("欢迎顾客！");
            while (true){
                System.out.println("请选择要执行的操作：1）查看所有宠物 2）购买宠物 3）退出");
                int num = scanner.nextInt();
                if (num == 1){
                    System.out.println("请选择排序方式：1.按价格  2.按年龄");
                    int num1 = scanner.nextInt();
                    if (num1 == 1){
                        select();
                    }
                    else if (num1 == 2){
                        select2();
                    }else {
                        System.out.println("请输入1~2的数字！");
                    }
                }else if (num == 2){
                    buy();
                }else if (num == 3){
                    break;
                }else {
                    System.out.println("请输入1~3的数字！！");
                }
            }
        }
        else{
            System.out.println("登录失败");
        }
    }
}



